package idv.arthur.work;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryBSS extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryBSS() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		String strSQL = "SELECT COUNT(bssid) FROM bss";
		String fromCall = request.getParameter("fc");
		ArrayList<InfoStruc> infoStrucs = new ArrayList<InfoStruc>();
		
		ResultTableSet resultTableSet = new ResultTableSet();
		
		int page = 1;
		int total_pages = 1;
		int count  = 0;
		int limit = 10;
		
		try {
			conn = wid.getConn(true);
			if ("host".equalsIgnoreCase(fromCall) ) {
				strSQL = "SELECT bssid,bssname FROM bss ORDER BY bssid ASC";
				ps = conn.prepareStatement(strSQL);
				rs = ps.executeQuery();
				while (rs.next()) {
					InfoStruc infoStruc = new InfoStruc();
					infoStruc.setId(rs.getString("bssid"));
					infoStruc.setName( wid.UI2DB( rs.getString("bssname"),conn.getMetaData().getDatabaseProductName() ) );
					infoStrucs.add(infoStruc);
				}
			} else {
				ps = conn.prepareStatement(strSQL);
				rs = ps.executeQuery();
				rs.next();
				count  = rs.getInt(1);
				if ( count  != 0 ) {
					if (limit  < count) {
						total_pages = (count / limit)+1;	
					}
					
					strSQL = "SELECT bssid,bssname FROM bss ORDER BY bssid ASC";
					ps = conn.prepareStatement(strSQL);
					rs = ps.executeQuery();
					ArrayList<Rows> rows = new ArrayList<Rows>();
					
					while ( rs.next() ) {
						final String bssId = rs.getString("bssid"); 
						Rows row = new Rows();
						row.setId(bssId);
						String[] cell = new String[2];
						cell[0] = bssId;
						cell[1] = wid.DB2UI( rs.getString("bssname"),conn.getMetaData().getDatabaseProductName() );
						row.setCell(cell);
						rows.add(row);
					}
					resultTableSet.setRows(rows);
				} else {
					page =0;
					total_pages=0;
				}
				resultTableSet.setPage(String.valueOf(page));
				resultTableSet.setTotal(total_pages);
				resultTableSet.setRecords(String.valueOf(count));	
			}
		} catch ( SQLException e) {
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch ( UnsupportedEncodingException e) {
			wid.kem(conn, ps, e, wid.toEType("UnsupportedEncodingException")+errPars, wid.isPrint2Console );
		} catch ( Exception e) {
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if ( rs != null ) { rs.close(); }
				if ( ps!= null ) { ps.close(); }
				if ( conn!= null ) { conn.close(); }
			} catch (SQLException e) {
				System.out.println( wid.errProcess(e) );
			}
		}
		
		
		
		if ("host".equalsIgnoreCase(fromCall)) {
			response.getWriter().write(new Gson().toJson(infoStrucs));
		} else {
			response.getWriter().write(new Gson().toJson(resultTableSet));	
		}
	}
}